package com.tongzhan.base;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @create 20200522
 * @author zhilai.xu
 */
public class RegUtil {
    /**
     * 验证5位整数 或者5位整数2位小数
     * @param param
     * @return
     */
    public static boolean regFiveTwo(String param){
        String reg ="(^[0-9]{1,5}$)|(^[0-9]{1,5}[\\.]{1}[0-9]{1,2}$)";
        boolean result = Pattern.matches(reg, param);
        return result;
    }

    /**
     * 验证3位整数 或者3位整数2位小数
     * @param param
     * @return
     */
    public static boolean regThreeTow(String param){
        String reg ="(^[0-9]{1,3}$)|(^[0-9]{1,3}[\\.]{1}[0-9]{1,2}$)";
        boolean result = Pattern.matches(reg, param);
        return result;
    }
    /**
     * 验证3位整数
     * @param param
     * @return
     */
    public static boolean regThree(String param){
        String reg ="(^[0-9]{1,3}$)";
        boolean result = Pattern.matches(reg, param);
        return result;
    }

    /**
     * 验证楼层 可以为负数
     * @param param
     * @return
     */
    public static boolean regThreeFloor(String param){
        String reg ="(^[0-9]{1,3}$)|(^-[0-9]{1,2}$)";
        boolean result = Pattern.matches(reg, param);
        return result;
    }
    /**
     * 验证4位整数
     * @param param
     * @return
     */
    public static boolean regFour(String param){
        String reg ="(^[0-9]{1,4}$)";
        boolean result = Pattern.matches(reg, param);
        return result;
    }

    /**
     * 验证精度
     * @param param
     * @return
     */
    public static boolean regLongtitue(String param){
        String reg ="(^[0-9]{1,3}$)|(^[0-9]{1,3}[\\.]{1}[0-9]{1,6}$)";
        boolean result = Pattern.matches(reg, param);
        return result;
    }

    /**
     * 验证纬度
     * @param param
     * @return
     */
    public static boolean regLatitude(String param){
        String reg ="(^[0-9]{1,2}$)|(^[0-9]{1,2}[\\.]{1}[0-9]{1,6}$)";
        boolean result = Pattern.matches(reg, param);
        return result;
    }

    /**
     * 验证日期（年月日）
     *
     * @param birthday 日期，格式：1992-09-03，或1992.09.03
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean checkBirthday(String birthday) {
//        ^\d{4}-\d{1,2}-\d{1,2}$
        String regex = "(^[0-9]{4}[\\-][0-9]{1,2}[\\-][0-9]{1,2}$)";
        return Pattern.matches(regex, birthday);
    }
    public static boolean checkMobile(String mobile) {
        String regex = "(\\+\\d+)?1[34578]\\d{9}$";
        return Pattern.matches(regex, mobile);
    }

    /**
     * 验证参数是否为数字,是true
     * @param object
     * @return
     */
    public final static boolean isNumber(Object object) {
        try {
            Pattern p = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
            Matcher m = p.matcher(object.toString());
            return m.matches();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 验证是否是手机号 是true
     * @param value
     * @return
     */
    public static boolean isMobilePhone(String value) {
        String mobilePhonePattern = "^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9])|(19[0-9]))\\d{8}$";
        Pattern p = Pattern.compile(mobilePhonePattern);
        Matcher m = p.matcher(value);
        return m.matches();
    }

    /**
     * 验证是否是邮箱
     * @param value
     * @return
     */
    public static boolean isEmail(String value) {
        String emailPattern = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
        Pattern p = Pattern.compile(emailPattern);
        Matcher m = p.matcher(value);
        return m.matches();
    }
}
